/*
 * @Author: liuying 247220246@qq.com
 * @Date: 2025-02-13 11:10:53
 * @LastEditors: liuying 247220246@qq.com
 * @LastEditTime: 2025-02-13 11:47:01
 * @FilePath: \my-book-react\src\hooks\useUpdateIsomorphicLayoutEffect\index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React from 'react';
import useIsomorphicLayoutEffect from '@/hooks/useIsomorphicLayoutEffect';
// 定义一个useUpdateIsomorphicLayoutEffect函数，用于在组件挂载和更新时执行回调函数
const useUpdateIsomorphicLayoutEffect = (callback: React.EffectCallback, deep?: React.DependencyList) => {
  // 创建一个ref，用于判断组件是否已经挂载
  const isMounted = React.useRef<boolean>(false);
  // 在组件挂载时，将isMounted设置为false
  useIsomorphicLayoutEffect(() => {
    return () => {
      isMounted.current = false;
    };
  }, []);
  // 在组件更新时，如果isMounted为false，则将isMounted设置为true，否则执行回调函数
  useIsomorphicLayoutEffect(() => {
    if (!isMounted.current) {
      isMounted.current = true;
    } else {
      callback();
    }
  }, deep);
};
export default useUpdateIsomorphicLayoutEffect;
